﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace RobotParser
{
    public partial class MainForm : Form
    {
        private RobotParser parser;
        private RobotScanner scanner;
        
        public MainForm()
        {
            InitializeComponent();

            scanner = new RobotScanner();
            parser = new RobotParser(scanner);

            parser.ParseError += OnParserErrorEventHandler;
        }

        private void ParseFile(string filePath)
        {
            this.Text = "RobotParser - " + filePath;
            loggerList.Items.Clear();            
            textBox.Clear();

            scanner.SetText(File.ReadAllText(filePath));

            bool instructionOk = parser.Parse();

            textBox.Text = instructionOk ? "OK" : "ERROR";
            textBox.Text += "\n" + parser.GetCount();;
        }

        public void OnParserErrorEventHandler(object sender, SimpleEventArgs args)
        {
            loggerList.Items.Add(args.Info);
        }
     
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.CheckPathExists = true;
            ofd.CheckFileExists = true;
            ofd.AddExtension = true;
            ofd.Filter = "Robot files(.robot)|*.robot|Simple text files(.txt)|*.txt|All files|(*.*)";

            if (ofd.ShowDialog(this) == DialogResult.OK)
            {
                ParseFile(ofd.FileName);
            }
        }
    }
}
